<!--
 * @Description:
 * @Author: yyh
 * @Date: 2021-06-24 16:10:48
 * @LastEditors: yyh
 * @LastEditTime: 2021-06-27 08:42:43
-->
<template>
  <!-- 顾客管理 -->
  <div class="customer_list">
    <!-- 添加按钮、模糊搜索、查询按钮 -->
    <div class="search">
      <div>
        <el-button class="btn" type="primary" @click="toAdd">添加</el-button>
      </div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item>
          <el-input
            v-model="searchForm.username"
            clearable
            placeholder="用户名"
          />
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchForm.status" clearable placeholder="状态">
            <el-option label="正常" value="正常" />
            <el-option label="禁用" value="禁用" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表单 -->
    <div class="form">
      <el-table v-loading="loading" :data="tableData" border style="width: 100%">
        <el-table-column width="50" prop="id" label="编号" />
        <el-table-column prop="username" label="用户名" />
        <el-table-column prop="realname" label="姓名" />
        <el-table-column prop="telephone" label="手机号" />
        <el-table-column prop="gender" label="性别">
          <template slot-scope="scope">
            <div v-if="scope.row.gender">
              <i v-if="scope.row.gender==='male'" class="el-icon-male green_info" />
              <i v-else class="el-icon-female red_info" />
            </div>
            <div v-else>暂无</div>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.status === '正常' ? 'success' : 'danger'"
            >{{ scope.row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="150">
          <template slot-scope="scope">
            <!-- {{scope.row}} -->
            <el-button type="text" size="small" @click="toEdit(scope.row)">编辑</el-button>
            <el-button style="color: red" type="text" size="small" @click="toDelete(scope.row.id)">删除</el-button>
            <el-button type="text" size="small" @click="toDetails(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="pagination-div">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[2, 5, 10, 15, 20, 30]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 模态框 对话框 -->
    <el-dialog :title="title" :visible.sync="visible" @close="toClose">
      <el-form ref="ruleForm" size="medium" :model="form" :rules="rules">
        <el-form-item
          prop="username"
          label="用户名"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="realname"
          label="姓名"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.realname" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="telephone"
          label="手机号"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.telephone" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="password"
          label="密码"
          :label-width="formLabelWidth"
        >
          <!-- show-password 密码输入时的显示与隐藏 -->
          <el-input
            v-model="form.password"
            autocomplete="off"
            show-password
          />
        </el-form-item>
        <el-form-item label="性别" prop="gender" :label-width="formLabelWidth">
          <el-radio-group v-model="form.gender">
            <el-radio label="male" class="el-icon-male">男</el-radio>
            <el-radio label="female" class="el-icon-female">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-if="title == '编辑用户信息'"
          prop="status"
          label="状态"
          :label-width="formLabelWidth"
        >
          <el-radio-group v-model="form.status">
            <el-radio :label="'禁用'">禁用</el-radio>
            <el-radio :label="'正常'">正常</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  pageQuery,
  addUserWidthRole,
  saveOrUpdate,
  deleteById
} from '@/api/user'

export default {
  data() {
    return {
      // 表格展示的数据
      tableData: [],
      // 分页参数
      params: {
        page: 1,
        pageSize: 10,
        roleId: 6
      },
      // 总条数
      total: 0,
      // 加载
      loading: false,
      // 模态框的显示与隐藏
      // 隐藏
      visible: false,
      // 表单数据
      form: {},
      // 表单控件标题的宽度
      formLabelWidth: '80px',
      // 模态框的标题
      title: '',
      // 模糊搜索表单的数据
      searchForm: {
        name: '',
        status: ''
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          }
        ],
        realname: [
          {
            required: true,
            message: '请输入姓名',
            trigger: 'blur'
          }
        ],
        telephone: [
          {
            required: true,
            message: '请输入手机号',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }
        ],
        status: [
          {
            required: true,
            message: '请选择状态',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {},
  // 深度监听分页参数
  watch: {
    params: {
      handler() {
        this.queryData()
      },
      deep: true
    }
  },
  created() {
    this.queryData()
  },
  mounted() {},
  methods: {
    // 分页模糊获取顾客数据
    async queryData() {
      this.loading = true
      // 处理数据
      const temp = {
        ...this.params,
        ...this.searchForm
      }
      // 遍历对象
      for (const key in temp) {
        console.log(key, temp[key])
        if (!temp[key]) {
          delete temp[key]
        }
      }
      const res = await pageQuery(temp)
      // 加载完毕
      this.loading = false
      this.tableData = res.data.list
      this.total = res.data.total
    },
    // 每页显示的条数发生变化时的事件处理程序
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      // this.params.pageSize = val;
      // this.params.page = 1;
      this.params = {
        page: 1,
        pageSize: val,
        roleId: 6
      }
      // this.queryData()
    },
    // 当前页码发生变化时的事件处理程序
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.page = val
      // this.queryData()
    },
    // 添加按钮事件处理程序
    toAdd() {
      // this.queryCategoryData();
      // 清空表单数据
      this.form = {}
      this.title = '添加顾客信息'
      // 显示模态框
      this.visible = true
    },
    // 查询按钮点击事件处理程序
    toSearch() {
      this.queryData()
    },
    // 编辑按钮事件处理程序
    toEdit(row) {
      this.queryData()
      // 设置表单数据
      this.form = { ...row };
      (this.title = '编辑用户信息'), (this.visible = true)
    },
    // 删除按钮事件处理程序
    toDelete(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async(valid) => {
          const res = await deleteById({ id })
          if (valid) {
            // 提示用户删除成功
            this.$notify({
              title: '删除成功',
              message: res.message,
              type: 'success'
            })
            this.queryData()
            // this.toClose();
            this.visible = false
          } else {
            // 提示用户删除失败
            this.$notify({
              title: '删除失败',
              message: res.message,
              type: 'error'
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 模态框确定按钮事件处理程序
    toSave() {
      // 获取dom的值，对值进行校验 form.name
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          // 校验通过，做自己的业务，保存
          // console.log("校验通过");
          // this.form;
          const temp = {
            ...this.form,
            roleId: 6
          }
          if (temp.id == null) {
            const res = await addUserWidthRole(temp)
            // console.log(res, "-----");
            // 提示用户保存成功，刷新表格数据，关闭模态框
            this.$notify.success({
              title: '成功',
              message: res.message
            })
            this.queryData()
            // this.toClose();
            this.visible = false
          } else {
            const res = await saveOrUpdate(temp)
            // console.log(res, "-----");
            // 提示用户保存成功，刷新表格数据，关闭模态框
            this.$notify.success({
              title: '成功',
              message: res.message
            })
            this.queryData()
            // this.toClose();
            this.visible = false
          }
        } else {
          // 校验不通过
          return false
        }
      })
    },
    // 关闭模态框
    toClose() {
      this.$refs.ruleForm.resetFields()
    },
    // 跳转到详情页面，查看详情
    toDetails(row) {
      // console.log(row);
      this.$router.push({
        path: '/admin/custome/Details',
        query: { id: row.id }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.search {
  display: flex;
  .btn {
    margin-right: 10px;
  }
}
</style>
